package com.wei.pdf;

import java.io.FileOutputStream;
import java.io.OutputStream;

import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFWriter;

public class FormatWriterdbf {

	public static void generateDbfFromArray(String dbfName,String[] strutName,byte[] strutType,int[] strutLength,Object[][] data)
	{   
		OutputStream fos = null;   
		try  
		{
			int fieldCount = strutName.length;   
			DBFField[] fields = new DBFField[fieldCount];   
			for(int i=0;i<fieldCount;i++)   
			{   
				fields[i] = new DBFField();   
				fields[i].setName(strutName[i]);   
				fields[i].setDataType(strutType[i]);   
				fields[i].setFieldLength(strutLength[i]);   
			}   
			DBFWriter writer = new DBFWriter();
			writer.setFields(fields);  
			writer.setCharactersetName("gb2312");
			
			for(int i=0;i<fieldCount;i++)   
			{   
				writer.addRecord(data[i]);   
			}   
			fos = new FileOutputStream(dbfName);   
			writer.write(fos);   
		}   
		catch(Exception e)   
		{   
			e.printStackTrace();   
		}   
		finally  
		{   
			try{   
				fos.close();   
			}catch(Exception e){e.printStackTrace();}   
		}   
	}  	

	public static void main(String[] args) {
		String[] strutName ={"ss","dd"};
		byte[] strutType ={DBFField.FIELD_TYPE_C,DBFField.FIELD_TYPE_C};
		int[] strutLength ={100,100};
		String[][] data ={{"各个各个各个各个各个","22"},{"333","444"}};
		FormatWriterdbf.generateDbfFromArray("F:/test.dbf", strutName, strutType, strutLength, data);
	}
}
